﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManageBanking.Models
{
    public class TotalMoneyModels
    {
        OnlineBankingDataContext olbk = new OnlineBankingDataContext();
        public decimal ttmoney(string card)
        {
            decimal kq = 0;
           try
           {
               // true: Nap
               // false: Rut
               
               decimal dep = 0;
               decimal with = 0;
               decimal transfer = 0;
               decimal receive = 0;
               decimal chepue = 0;
               if (olbk.Deposit_Withs.Where(ab => ab.IdAccount == card && ab.IDType == true).Sum(ab => ab.Money).HasValue == true)
                   dep = olbk.Deposit_Withs.Where(ab => ab.IdAccount == card && ab.IDType == true).Sum(ab => ab.Money).Value;
               if (olbk.Deposit_Withs.Where(ab => ab.IdAccount == card && ab.IDType == false).Sum(ab => ab.Money).HasValue == true)
                   with = olbk.Deposit_Withs.Where(ab => ab.IdAccount == card && ab.IDType == false).Sum(ab => ab.Money).Value;
               if (olbk.Transfers.Where(ab => ab.IdAccountFrom == card).Sum(ab => ab.Money).HasValue==true)
                   transfer = olbk.Transfers.Where(ab => ab.IdAccountFrom == card).Sum(ab => ab.Money).Value;
               if (olbk.Transfers.Where(ab => ab.IdAccountTo == card && ab.Solve == true).Sum(ab => ab.Money).HasValue == true)
                   receive = olbk.Transfers.Where(ab => ab.IdAccountTo == card && ab.Solve==true ).Sum(ab => ab.Money).Value;
               if(olbk.Cheques.Where(ab => ab.IdAccount == card).Sum(ab => ab.Money).HasValue==true)
                  chepue = olbk.Cheques.Where(ab => ab.IdAccount == card).Sum(ab => ab.Money).Value;
               kq = dep - with - transfer + receive - chepue;
               return kq;
           }
           catch
           {
              return kq;
           }
        }
    }
}
